<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServiceController
 *
 * @author nmthong
 */

class LocationController extends Controller{
    // Members
    /**
     * Key which has to be in HTTP USERNAME and PASSWORD headers 
     */
    Const APPLICATION_ID = 'ASCCPE';
 
    /**
     * Default response format
     * either 'json' or 'xml'
     */
    private $format = 'json';
    /**
     * @return array action filters
     */    
public $marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
,"ế","ệ","ể","ễ",
"ì","í","ị","ỉ","ĩ",
"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
,"ờ","ớ","ợ","ở","ỡ",
"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
"ỳ","ý","ỵ","ỷ","ỹ",
"đ",
"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
"Ì","Í","Ị","Ỉ","Ĩ",
"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
,"Ờ","Ớ","Ợ","Ở","Ỡ",
"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
"Đ");

public $marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
,"a","a","a","a","a","a",
"e","e","e","e","e","e","e","e","e","e","e",
"i","i","i","i","i",
"o","o","o","o","o","o","o","o","o","o","o","o"
,"o","o","o","o","o",
"u","u","u","u","u","u","u","u","u","u","u",
"y","y","y","y","y",
"d",
"A","A","A","A","A","A","A","A","A","A","A","A"
,"A","A","A","A","A",
"E","E","E","E","E","E","E","E","E","E","E",
"I","I","I","I","I",
"O","O","O","O","O","O","O","O","O","O","O","O"
,"O","O","O","O","O",
"U","U","U","U","U","U","U","U","U","U","U",
"Y","Y","Y","Y","Y",
"D");    
    
    
    
        public function actionUpdateLocation(){
            try{
                $username=Yii::app()->request->getQuery('username');
                $lac=Yii::app()->request->getQuery('lac');
                $cid=Yii::app()->request->getQuery('cid');
                $mnc=Yii::app()->request->getQuery('mnc');
                $mcc=Yii::app()->request->getQuery('mcc');
                $direction=Yii::app()->request->getQuery('direction');
                if(!isset($direction)){
                    $direction=0;
                }
                if (isset($lac) && isset($cid) && isset($mnc) && isset($mcc)) {
                  $data =
                                "\x00\x0e".
                                "\x00\x00\x00\x00\x00\x00\x00\x00".
                                "\x00\x00".
                                "\x00\x00".
                                "\x00\x00".
                                "\x1b".
                                "\x00\x00\x00\x00". 
                                "\x00\x00\x00\x00". 
                                "\x00\x00\x00\x00". 
                                "\x00\x00".
                                "\x00\x00\x00\x00". 
                                "\x00\x00\x00\x00". 
                                "\x00\x00\x00\x00". 
                                "\x00\x00\x00\x00". 
                                "\xff\xff\xff\xff".
                                "\x00\x00\x00\x00";

                  $is_umts_cell = ($cid > 65535);
                  if ($is_umts_cell) // GSM: 4 hex digits, UTMS: 6 hex digits
                   $data[0x1c] = 5;
                  else
                   $data[0x1c] = 3;

                  $hexmcc = substr("00000000".dechex($_REQUEST["mcc"]),-8);
                  $hexmnc = substr("00000000".dechex($_REQUEST["mnc"]),-8);
                  $hexlac = substr("00000000".dechex($_REQUEST["lac"]),-8);
                  $hexcid = substr("00000000".dechex($_REQUEST["cid"]),-8);

                 // echo "<p>
                //MCC=$hexmcc MNC=$hexmnc LAC=$hexlac CID=$hexcid";

                  $data[0x11] = pack("H*",substr($hexmnc,0,2));
                  $data[0x12] = pack("H*",substr($hexmnc,2,2));
                  $data[0x13] = pack("H*",substr($hexmnc,4,2));
                  $data[0x14] = pack("H*",substr($hexmnc,6,2));

                  $data[0x15] = pack("H*",substr($hexmcc,0,2));
                  $data[0x16] = pack("H*",substr($hexmcc,2,2));
                  $data[0x17] = pack("H*",substr($hexmcc,4,2));
                  $data[0x18] = pack("H*",substr($hexmcc,6,2));

                  $data[0x27] = pack("H*",substr($hexmnc,0,2));
                  $data[0x28] = pack("H*",substr($hexmnc,2,2));
                  $data[0x29] = pack("H*",substr($hexmnc,4,2));
                  $data[0x2a] = pack("H*",substr($hexmnc,6,2));

                  $data[0x2b] = pack("H*",substr($hexmcc,0,2));
                  $data[0x2c] = pack("H*",substr($hexmcc,2,2));
                  $data[0x2d] = pack("H*",substr($hexmcc,4,2));
                  $data[0x2e] = pack("H*",substr($hexmcc,6,2));

                  $data[0x1f] = pack("H*",substr($hexcid,0,2));
                  $data[0x20] = pack("H*",substr($hexcid,2,2));
                  $data[0x21] = pack("H*",substr($hexcid,4,2));
                  $data[0x22] = pack("H*",substr($hexcid,6,2));

                  $data[0x23] = pack("H*",substr($hexlac,0,2));
                  $data[0x24] = pack("H*",substr($hexlac,2,2));
                  $data[0x25] = pack("H*",substr($hexlac,4,2));
                  $data[0x26] = pack("H*",substr($hexlac,6,2));

                  /* I used file_get_contents() at my laptop webserver, but it seems like the PHP version
                   * at my hosting company is old and it is not supporting that.
                   * For the hosting company, here we're using cURL.
                   */
                  $use_curl = true;
                  if ($use_curl) {
                   $ch = curl_init();
                   curl_setopt($ch, CURLOPT_URL, "http://www.google.com/glm/mmap");
                   curl_setopt($ch, CURLOPT_HEADER, true);
                   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                   curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
                   curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-type: application/binary"));
                   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                   curl_setopt($ch, CURLOPT_POST, 1);
                   $response = curl_exec($ch);
                   if (curl_errno($ch))
                    exit("Error: Failed to post data $str curl_errno($ch)");

                   $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
                   $str = substr($response, $header_size);

                   curl_close($ch);
                  } else {
                   $context = array (
                    'http' => array (
                     'method' => 'POST',
                     'header'=> "Content-type: application/binary\r\n"
                        . "Content-Length: " . strlen($data) . "\r\n",
                        'content' => $data
                    )
                   );
                   $xcontext = stream_context_create($context);
                   $str=file_get_contents("http://www.google.com/glm/mmap", FALSE, $xcontext);
                  }

                  $opcode1 = ((ord($str[0]) << 8)) | ord($str[1]);
                  $opcode2 = ord($str[2]);

                  if (($opcode1 != 0x0e) || ($opcode2 != 0x1b))
                   exit("<p>Error: Invalid opcode $opcode1 $opcode2. Maybe the LAC/CID is invalid");

                  $retcode = ((ord($str[3]) << 24) | (ord($str[4]) << 16) | (ord($str[5]) << 8) | (ord($str[6])));
                  if ($retcode != 0)
                   exit("<p>Error: Invalid return code $retcode. Maybe the LAC/CID is invalid");

                  $lat = ((ord($str[7]) << 24) | (ord($str[8]) << 16) | (ord($str[9]) << 8) | (ord($str[10]))) / 1000000;
                  $lon = ((ord($str[11]) << 24) | (ord($str[12]) << 16) | (ord($str[13]) << 8) | (ord($str[14]))) / 1000000;

                  // exit script if cannot geocode cell e.g. not on google's database
                  if ($lat == 0 and $lon == 0)
                   exit('ERROR: cannot determine cell tower location from cell LAC: ' . $_REQUEST["lac"] . ', ' . 'CID: ' . $_REQUEST["cid"]);

                  $addr = $lat . ',' . $lon;
                  $t = urlencode(" (MCC=$mcc MNC=$mnc LAC=$lac CID=$cid)");
                  $json=array('location'=>$lat.','.$lon);
                  $string=json_encode($json);
                 // echo $string;
                  //echo "<p><a href='http://maps.google.com/maps?q=$addr$t' target='_top'>Lat=$lat Lon=$lon</a>";
                 //get addresss
  
                  $ch = curl_init();
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_ENCODING, "");
                    curl_setopt($ch, CURLOPT_HTTPHEADER ,array(
                            'Content-Type: html/text; charset=utf-8',
                  ));
                  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
                  curl_setopt($ch, CURLOPT_URL, 
                            "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lon."&sensor=true"
                  );
                  $content = curl_exec($ch);
                  curl_close($ch);
                  $json=json_decode($content);
                  $address=$json->results[0]->formatted_address;
                  $address=  str_replace($this->marTViet,$this->marKoDau,$address);
                  $country_code=end($json->results[0]->address_components)->short_name;
                  $country_name=end($json->results[0]->address_components)->long_name;

                   
                 }    
                

                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="Select ls_users.lat,ls_users.lng from ls_users where ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   

                $location= $rows_user[0];               

                $query="UPDATE ls_users SET ls_users.direction=:direction,ls_users.lat=:lat,ls_users.lng=:lng,ls_users.lat_past=:lat_past,ls_users.lng_past=:lng_past,ls_users.location=:location,ls_users.country_code=:country_code,ls_users.last_time=:last_time WHERE ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $command->bindParam(":direction",$direction,PDO::PARAM_INT);
                $command->bindParam(":lat",$lat,PDO::PARAM_STR);
                $command->bindParam(":lng",$lon,PDO::PARAM_STR);
                $command->bindParam(":lat_past",$location["lat"],PDO::PARAM_STR);
                $command->bindParam(":lng_past",$location["lng"],PDO::PARAM_STR);                
                $command->bindParam(":location",$address,PDO::PARAM_STR);
                $command->bindParam(":country_code",$country_code,PDO::PARAM_STR);
                
                $last_time=time();
                $command->bindParam(":last_time",$last_time,PDO::PARAM_STR);                
                $row_count=$command->execute();
                $result=array('data'=>'','status'=>'ok');
                $string=json_encode($result);
                echo $string;
           
                  
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }            
        }
        
        public function actionGetLocation()
        {
            try{
                $username=Yii::app()->request->getQuery('username');
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="Select DISTINCT ls_users.lat,ls_users.lng,ls_users.lat_past,ls_users.lng_past,ls_users.location,ls_users.direction from ls_users where ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   

                $location= $rows_user[0];
                $string=json_encode($location);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
        }      
        
        
        public function actionGetDistance()
        {
            try{
                $from=Yii::app()->request->getQuery('from');
                $to=Yii::app()->request->getQuery('to');
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="Select DISTINCT ls_users.lat,ls_users.lng from ls_users where ls_users.username='".$from."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   
                $location= $rows_user[0];
                $lat1=$location["lat"];
                $lng1=$location["lng"];

                $query="Select DISTINCT ls_users.lat,ls_users.lng from ls_users where ls_users.username='".$to."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   
                $location= $rows_user[0];
                $lat2=$location["lat"];
                $lng2=$location["lng"];
                $distance=$this->getDistanceBetweenPoints($lat1,$lng1,$lat2,$lng2,'m');
                echo $distance;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
        }   
        
        function getDistanceBetweenPoints($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'm') {
            $theta = $longitude1 - $longitude2;
            $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
            $distance = acos($distance);
            $distance = rad2deg($distance);
            $distance = $distance * 60 * 1.1515; 
            switch($unit) {
                    case 'mi': break; 
                    case 'km' : $distance = $distance * 1.609344; break;
                    case 'm' : $distance = $distance * 1609.344;  break;  
            }
            return (round($distance,2));
        }

    
        public function actionUpdateLatLng(){
            try{
                $username=Yii::app()->request->getQuery('username');
                $lat=Yii::app()->request->getQuery('lat');
                $lng=Yii::app()->request->getQuery('lng');
                $direction=Yii::app()->request->getQuery('direction');
                if(!isset($direction)){
                    $direction=0;
                }
  
                  $ch = curl_init();
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_ENCODING, "");
                    curl_setopt($ch, CURLOPT_HTTPHEADER ,array(
                            'Content-Type: html/text; charset=utf-8',
                  ));
                  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
                  curl_setopt($ch, CURLOPT_URL, 
                            "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true"
                  );
                  $content = curl_exec($ch);
                  curl_close($ch);
                  $json=json_decode($content);
                  $address=$json->results[0]->formatted_address;
                  $address=  str_replace($this->marTViet,$this->marKoDau,$address);
                  $country_code=end($json->results[0]->address_components)->short_name;
                  $country_name=end($json->results[0]->address_components)->long_name;

                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="Select ls_users.lat,ls_users.lng from ls_users where ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   

                $location= $rows_user[0]; 

                //$connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="UPDATE ls_users SET ls_users.direction=:direction,ls_users.lat=:lat,ls_users.lng=:lng,ls_users.lat_past=:lat_past,ls_users.lng_past=:lng_past,ls_users.location=:location,ls_users.country_code=:country_code,ls_users.last_time=:last_time WHERE ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $command->bindParam(":direction",$direction,PDO::PARAM_INT);
                $command->bindParam(":lat",$lat,PDO::PARAM_STR);
                $command->bindParam(":lng",$lng,PDO::PARAM_STR);
                $command->bindParam(":lat_past",$location["lat"],PDO::PARAM_STR);
                $command->bindParam(":lng_past",$location["lng"],PDO::PARAM_STR);
                $command->bindParam(":location",$address,PDO::PARAM_STR);
                $command->bindParam(":country_code",$country_code,PDO::PARAM_STR);
                $last_time=time();
                $command->bindParam(":last_time",$last_time,PDO::PARAM_STR);                
                $row_count=$command->execute();
                $result=array('data'=>'','status'=>'ok');
                $string=json_encode($result);
                echo $string;
           
                  
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }            
        }
        
        public function actionGetDistanceFromStation(){
            try{
                $username=Yii::app()->request->getQuery('username');
                $route_id=Yii::app()->request->getQuery('route_id');
                $point_id=Yii::app()->request->getQuery('point_id');
                $distanceFromStation=0;
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="Select DISTINCT ls_users.direction,ls_users.lat,ls_users.lng,ls_users.lat_past,ls_users.lng_past from ls_users where ls_users.username='".$username."'";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_user = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_user[]=$row;
                }   
                $location= $rows_user[0];
                $lat=$location["lat"];
                $lng=$location["lng"];
                $lat_past=$location["lat_past"];
                $lng_past=$location["lng_past"];
                $direction=$location["direction"];
                if($lng_past==0 || $lat_past==0) {
                    $result=array('data'=>'','status'=>'fail','error'=>'lack lat/lng in the past');
                    echo $string=json_encode($result);
                    return;
                }
                $query="Select * from ls_route where ls_route.id=".$route_id;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_route = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_route[]=$row;
                }                 
                $routes=$rows_route[0];
                $str=$routes["data"];
                $route=json_decode($str);
                $minDistance=0;
                $markPoint=0;
                for($i=0;$i<count($route)-1;$i++){
                    $pointA=$route[$i];
                    $pointB=$route[$i+1];
                    $distance=Util::DistancePointToLine($pointA->lat, $pointA->lng,$pointB->lat, $pointB->lng, $lat, $lng);
                    if($i==0){
                       $minDistance=$distance; 
                    }else if($minDistance>$distance){
                        $minDistance=$distance;
                        $markPoint=$i;
                    }
                    
                }
                
                $endPoint=$route[$point_id];
                //$direction=Util::Direction($lat_past, $lng_past, $lat, $lng, $route[0]->lat, $route[0]->lng);
                //chay theo huong thuan cua route (luot di)
                if($direction==0){
                    if($point_id<$markPoint){
                        if($endPoint->type==1){//tram o luot di
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                            $dist2=Util::getRealDistance2PointInRoute($markPoint+1, count($route)-1, $route);
                            $dist3=Util::getRealDistance2PointInRoute(0, count($route)-1, $route);
                            $dist4=Util::getRealDistance2PointInRoute(0, $point_id, $route);
                            $distanceFromStation=$dist1+$dist2+$dist3+$dist4;
                        }else{
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                            $dist2=Util::getRealDistance2PointInRoute($markPoint+1, count($route)-1, $route);
                            $dist3=Util::getRealDistance2PointInRoute($point_id, $markPoint+1, $route);
                            $distanceFromStation=$dist1+$dist2*2+$dist3;
                                      
                        }
                    }else if($point_id==$markPoint){
                        $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                        if($dist1<10){
                            $distanceFromStation=0;
                        }else{
                            if($endPoint->type==1){//tram o luot di
                                $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                                $dist2=Util::getRealDistance2PointInRoute($markPoint+1, count($route)-1, $route);
                                $dist3=Util::getRealDistance2PointInRoute(0, count($route)-1, $route);
                                $dist4=Util::getRealDistance2PointInRoute(0, $point_id, $route);
                                $distanceFromStation=$dist1+$dist2+$dist3+$dist4;
                            }else{
                                $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                                $dist2=Util::getRealDistance2PointInRoute($markPoint+1, count($route)-1, $route);
                                $dist3=Util::getRealDistance2PointInRoute($point_id, $markPoint+1, $route);
                                $distanceFromStation=$dist1+$dist2*2+$dist3;

                            }                            
                        }
                    }else if($point_id>$markPoint){
                            if($endPoint->type==2){//tram o luot ve
                                $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                                $dist2=Util::getRealDistance2PointInRoute($markPoint+1, count($route)-1, $route);
                                $dist3=Util::getRealDistance2PointInRoute($point_id, count($route)-1, $route);
                                $distanceFromStation=$dist1+$dist2+$dist3;
                            }else {
                                $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                                $dist2=Util::getRealDistance2PointInRoute($markPoint+1, $point_id, $route);
                                $distanceFromStation=$dist1+$dist2;

                            }                         
                    }
                }else{
                    //chay theo huong ngược cua route (luot ve)
                    if($point_id<$markPoint){
                        if($endPoint->type==1){//tram o luot di
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint]->lat, $route[$markPoint]->lng);
                            $dist2=Util::getRealDistance2PointInRoute(0, $markPoint, $route);
                            $dist3=Util::getRealDistance2PointInRoute(0, $point_id, $route);
                            $distanceFromStation=$dist1+$dist2+$dist3;
                        }else{
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint+1]->lat, $route[$markPoint+1]->lng);
                            $dist2=Util::getRealDistance2PointInRoute($markPoint, $point_id, $route);
                            
                            $distanceFromStation=$dist1+$dist2;
                                      
                        }                        
                    }else if($point_id==$markPoint){
                        if($endPoint->type==1){//tram o luot di
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint]->lat, $route[$markPoint]->lng);
                            $dist2=Util::getRealDistance2PointInRoute(0, $markPoint, $route);
                            $distanceFromStation=$dist1+$dist2*2;
                        }else{
                            $distanceFromStation=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint]->lat, $route[$markPoint]->lng);
                                     
                        }                          
                    }else if($point_id>$markPoint){
                            $dist1=  Util::getRealDistanceBetweenPoints($lat, $lng, $route[$markPoint]->lat, $route[$markPoint]->lng);
                            $dist2=Util::getRealDistance2PointInRoute(0, $markPoint, $route);
                            $dist3=Util::getRealDistance2PointInRoute(0, $point_id, $route);
                            $distanceFromStation=$dist1+$dist2+$dist3;
                     
                    }                    
                }
                echo $distanceFromStation;
           
                  
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }            
        }
}

?>
